package com.douqu.game.core.config;

import com.douqu.game.core.config.common.GoodsData;
import com.douqu.game.core.entity.GameObject;
import com.douqu.game.core.entity.common.TwoTuple;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.LoadUtils;
import com.douqu.game.core.util.LogUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author: miracle.
 * Description:
 * Date: 2018/8/9 0009 下午 5:54
 * Huan Yu Copyright (c) 2017 All Rights Reserved.
 */
public class RankRewardConfig extends GameObject{

    /**
     * 排行榜类型
     */
    public int type;

    /**
     * 排名
     */
    public int num;

    /**
     * 奖励
     */
    public GoodsData[] reward;

    @Override
    public boolean check() {
        //遍历排行
        if ( type < 0){
            LogUtils.error("WorldMapRankRewardConfig check type error -> id:" + id + " type:" + type);
            return false;
        }

        if ( num <= 0){
            LogUtils.error("WorldMapRankRewardConfig check num error -> id:" + id + " num:" + num);
            return false;
        }

        if ( reward == null){
            LogUtils.error("WorldMapRankRewardConfig check reward is null -> id:" + id);
            return false;
        }

        return true;
    }

    @Override
    public boolean setVariable(String key, String value) {
        if ("reward".equals(key)){
            reward = LoadUtils.loadGoodDataToArray(key, value);
            return LoadUtils.checkGoods(reward, this.getClass(), key, id);
        }else
            return super.setVariable(key, value);
    }

    public static TwoTuple<List<RankRewardConfig>,Integer> getConfigListByType(SGCommonProto.E_RANK_TYPE type,Integer maxValue){
        List<RankRewardConfig> configList = new ArrayList<>();
        List<RankRewardConfig> rankRewardConfigList = DataFactory.getInstance().getDataList(DataFactory.RANK_REWARD_KEY);
        for (RankRewardConfig config:rankRewardConfigList) {
            if(config.type == type.getNumber()){
                configList.add(config);
                maxValue = maxValue > config.num?maxValue : config.num;
            }
        }
        configList.sort(new Comparator<RankRewardConfig>() {
            @Override
            public int compare(RankRewardConfig o1, RankRewardConfig o2) {
                return new Integer(o1.id).compareTo(o2.id);
            }
        });
        TwoTuple<List<RankRewardConfig>,Integer> twoTuple = new TwoTuple<>();
        twoTuple.setFirst(configList);
        twoTuple.setSecond(maxValue);
        return twoTuple;
    }
}
